'use strict';

const dayjs = require('dayjs');
const RouterService = require('../base');
const { Op } = require('sequelize');
class TravelSkuStockService extends RouterService {
  static get modelName() {
    return 'Business.TravelSkuStock';
  }

  async beforeCreate(data) {
    /**
     *   travel_sku_id: this.currentSku.id,
              travel_date: date,
     */
    // 校验是否存在
    const travelSkuStock = await this.app.model.Business.TravelSkuStock.findOne({
      where: {
        travel_sku_id: data.travel_sku_id,
        // 判断是否是同一天
        travel_date: {
          // 判断00:00:00 - 23:59:59
          [Op.between]: [dayjs(data.travel_date).startOf('day').toDate(), dayjs(data.travel_date).endOf('day').toDate()]
        }
      }
    });
    console.log(travelSkuStock, 'travelSkuStock111');
    if (travelSkuStock) {
      throw new Error('该日期已存在');
    }
    return data;
  }

  async batchDelete(ids) {
    if (!ids || ids.length === 0) {
      return true
    }
    const result = await this.app.model.Business.TravelSkuStock.destroy({
      where: {
        id: { [Op.in]: ids }
      }
    });
    return result;
  }
}

module.exports = TravelSkuStockService; 